package svc

import (
	"api/internal/config"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"github.com/gomodule/redigo/redis"
	"github.com/zeromicro/go-zero/core/logx"
	"time"
)

type ServiceContext struct {
	Config        config.Config
	RedisPool     redis.Pool
	OssCli        *oss.Client
	WebSocketPool *ConnectionPool
	//RedisConn redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
	client, err := oss.New(c.AliOss.Endpoint, c.AliOss.AccessKeyId, c.AliOss.AccessKeySecret)
	if err != nil {
		logx.Error("Failed to create OSS client: %v", err)
	}
	return &ServiceContext{
		Config: c,
		RedisPool: redis.Pool{
			Dial: func() (redis.Conn, error) {
				return redis.Dial("tcp", c.RedisPool.Host+":"+c.RedisPool.Port)
			},
			MaxIdle:     c.RedisPool.MaxIdle,
			MaxActive:   c.RedisPool.MaxActive,
			IdleTimeout: time.Duration(c.RedisPool.IdleTimeout),
		},
		OssCli:        client,
		WebSocketPool: NewConnectionPool(c.WsPool.MaxSize, c.WsPool.Url),
	}
}
